package com.zhuzi.demo.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.zhuzi.demo.dao.UserDao;
import com.zhuzi.demo.exception.MyException;
import com.zhuzi.demo.model.User;

@Service(value = "userService")
@Transactional(propagation=Propagation.REQUIRED,rollbackFor=MyException.class,isolation=Isolation.READ_COMMITTED)
public class UserService {
	@Autowired
    private UserDao userDao;

    public Map<String,Object> addUser(String userName,String password,String phone) throws MyException{
    	Map<String,Object> map = new HashMap<String,Object>();
    	User user = new User();
    	user.setUserName(userName);
    	user.setPassword(password);
    	user.setPhone(phone);
    	userDao.insert(user);
        return map;
    }

    /*
    * pageNum 开始页数
    * pageSize 每页显示的数据条数
    * */
    public Map<String,Object> findAllUser(int pageNum, int pageSize) throws MyException{
    	Map<String,Object> map = new HashMap<String,Object>();
        //将参数传给这个方法就可以实现物理分页了，非常简单。
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userDao.selectUsers();
        map.put("data",list);
        return map;
    }
}